home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-03-27 | 10.5 KB | 453 lines | [TEXT/MPS ] |
- {[a-,body+,h-,o=100,r+,rec+,t=4,u+,#+,j=20/57/1$,n+]}
-
- {------------------------------------------------------------------------------
- Simple framework for an MPW tool.
-
- NOTE - tools cannot currently be built with -debug… Sorry!
- ------------------------------------------------------------------------------}
- UNIT UMPWTool;
-
- INTERFACE
-
- USES
- { • MacApp }
- UMacApp,
-
- { • Building Blocks }
-
- { • Required for this unit's interface }
- UAssociation,
-
- { • Implementation use }
- CursorCtl, Signal, PasLibIntf, IntEnv, ErrMgr, Events, OSUtils, Memory, Resources, Fonts;
-
- CONST
- Version = '1.0'; { Current version}
- kErrorMarker = '### ';
-
- { Keyword IDs. Negative numbers reserved for the framework }
- kwP = -1;
- kwNoP = -2;
- kwT = -3;
- kwNoT = -4;
- kwHelp = -5;
-
- TYPE
- TMPWTool = OBJECT (TObject)
- fKeyWordList: TAssociation; { keywords to this command }
- fProgName: Str255; { Program's file name}
- fInterrupted: Boolean; { True ==> interrupted (Cmd "." pressed)}
- fCursorCount: integer; { for our spinning cursor}
- fRetCode: (RC_Normal, RC_ParmErrs, RC_DontMatch, RC_Abort); {Return codes}
- fProgress: Boolean; { true for progress request }
- fTime: Boolean; { true for elapsed time request }
- fStartTicks: Longint; { tickcount at start of tool }
- fStartDateTime: Longint; { Date/Time at start of tool }
- fArgVIndex: integer;
-
- PROCEDURE TMPWTool.ITool;
- PROCEDURE TMPWTool.InstallKeyWord(keyword: Str255; kw: Integer);
- FUNCTION TMPWTool.LookupKeyword(keyword: Str255; var kw: Integer): BOOLEAN;
- PROCEDURE TMPWTool.InstallKeyWords;
- PROCEDURE TMPWTool.DoProcessFileArg(arg: Str255);
- PROCEDURE TMPWTool.DoProcessOptionArg(kw: integer);
- PROCEDURE TMPWTool.DoShowUsage;
- PROCEDURE TMPWTool.DoStartProgress;
- PROCEDURE TMPWTool.DoToolAction;
- FUNCTION TMPWTool.GetNextArg: Str255;
- PROCEDURE TMPWTool.ProcessArg(arg: Str255);
- PROCEDURE TMPWTool.Run;
- PROCEDURE TMPWTool.Stop(msg: Str255);
- PROCEDURE TMPWTool.SyntaxError(suffix: Str255);
- END;
-
- PROCEDURE InitUMPWTool;
-
- {$IFC qTrace}
- {$Push} {$%+}
-
- PROCEDURE %_BP;
-
- PROCEDURE %_EP;
-
- PROCEDURE %_EX;
- {$Pop}
- {$ENDC}
-
- VAR
- gTool: TMPWTool; { The tool }
- gProgName: Str255; { Program's file name}
-
- IMPLEMENTATION
-
- {--------------------------------------------------------------------------------------------------}
-
- {$IFC qTrace}
- {$Push} {$%+}
- {$S Main}
-
- PROCEDURE %_BP;
-
- BEGIN
- END;
- {$S Main}
-
- PROCEDURE %_EP;
-
- BEGIN
- END;
- {$S Main}
-
- PROCEDURE %_EX;
-
- BEGIN
- END;
- {$Pop}
- {$ENDC}
-
- {--------------------------------------------------------------------------------------------------}
-
- {$S TInit}
-
- PROCEDURE InitUMPWTool;
-
- BEGIN
- { Do Tool related initialization }
- InitGraf(@thePort);
- SetFScaleDisable(true); { per chapter in MPW guide on tools }
-
- InitCursorCtl(NIL);
- RotateCursor(0);
-
- InitErrMgr('', '', false);
-
- gProgName := ArgV^[0]^;
-
- DefineConfiguration(gConfiguration);
-
- SetRGBColor(gRGBBlack, 0, 0, 0);
- SetRGBColor(gRGBWhite, $FFFF, $FFFF, $FFFF);
- gStrippedAddress := StripAddress(Ptr( - 1));
- gCursorRgn := NewRgn;
-
- gBoolString[true] := 'TRUE';
- gBoolString[FALSE] := 'FALSE';
- gDeadStripSuppression := FALSE;
- gCreateWithTemplates := gDeadStripSuppression; { for compatibility with Dave W. class notes
- }
- { The refnum where the application's resources should be found }
- gApplicationRefNum := CurResFile;
-
- gToolBoxInitialized := true;
-
- { the main procedure is always compiled with universal code so, the FPU must be reset before it
- is used. We could get spurious crashes or worse.
-
- Remember: 2+2=4… every time!
- }
-
- InitUPatch;
-
- { the following set up is necessary to call CleanupMacApp }
- gApplication := NIL;
-
- FailNil(gCursorRgn);
-
-
- { Do Object related initialization }
- InitUObject;
-
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TRes}
-
- PROCEDURE Intr;
-
- BEGIN
- gTool.fInterrupted := true; {we test this switch periodically}
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TRes}
-
- PROCEDURE TMPWTool.Stop(msg: Str255);
-
- BEGIN
- IF Length(msg) > 0 THEN
- BEGIN
- PLFlush(Output);
- WriteLn(Diagnostic);
- WriteLn(Diagnostic, msg);
- END;
-
- IF fInterrupted THEN
- IEexit( - 9);
- { don't worry about closing the files we opened. The Shell
- will do so if appropriate.}
- IEexit(Ord(fRetCode)); {exit, returning the appropriate status
- code}
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TMPWTool.SyntaxError(suffix: Str255);
-
- VAR
- aStr: Str255;
-
- BEGIN
- aStr := fProgName;
- PLFlush(Output);
- WriteLn(Diagnostic, kErrorMarker, 'Bad Parameter: ', suffix);
- WriteLn(Diagnostic, kErrorMarker, aStr, '<invalid option>');
- Stop('');
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TMPWTool.DoShowUsage;
-
- VAR
- aStr: Str255;
-
- BEGIN
- aStr := fProgName;
- WriteLn(Diagnostic, '# Usage: ', aStr, ' [-p]');
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- FUNCTION TMPWTool.GetNextArg: Str255;
-
- BEGIN
- fArgVIndex := fArgVIndex + 1;
- IF fArgVIndex > ArgC THEN
- Stop('Not enough arguments');
- GetNextArg := ArgV^[fArgVIndex]^;
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TMPWTool.InstallKeyWords;
-
- BEGIN
- InstallKeyWord('P', kwP);
- InstallKeyWord('NoP', kwNoP);
- InstallKeyWord('T', kwT);
- InstallKeyWord('NoT', kwNoT);
- InstallKeyWord('Help', kwHelp);
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TMPWTool.InstallKeyWord(keyword: Str255; kw: Integer);
- var
- value: Str255;
-
- BEGIN
- UprStr255(keyword);
- value[0] := chr(2);
- value[1] := chr(BSR(Band(kw, $FF00), 8));
- value[2] := chr(Band(kw, $00FF));
- fKeyWordList.InsertEntry(keyword, value);
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- FUNCTION TMPWTool.LookupKeyword(keyword: Str255; var kw: Integer): BOOLEAN;
- var
- value: Str255;
-
- BEGIN
- UprStr255(keyword);
- if fKeyWordList.ValueAt(keyword, value) then
- begin
- LookupKeyword := true;
- kw := BOR(BSL(ord4(value[1]), 8), ord(value[2]));
- end
- else
- LookupKeyword := false;
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TMPWTool.ProcessArg(arg: Str255);
- var
- akw: integer;
-
- BEGIN
- IF arg[1] <> '-' THEN
- DoProcessFileArg(arg)
- ELSE
- begin
- if LookupKeyWord(copy(arg, 2, Length(arg) - 1), akw) then
- DoProcessOptionArg(akw)
- else
- SyntaxError(Concat(ArgV^[fArgVIndex]^, ' <invalid option>'));
- end;
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TMPWTool.DoProcessFileArg(arg: Str255);
-
- BEGIN
- SyntaxError(Concat(ArgV^[fArgVIndex]^, ' <invalid option>'));
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TMPWTool.DoProcessOptionArg(kw: integer);
-
- BEGIN
- case kw of
- kwP:
- fProgress := true;
- kwNoP:
- fProgress := false;
- kwT:
- fTime := true;
- kwNoT:
- fTime := false;
- kwHelp:
- BEGIN
- DoShowUsage;
- fRetCode := RC_Normal;
- Stop('');
- END;
- otherwise
- SyntaxError(Concat(ArgV^[fArgVIndex]^, ' <invalid option>'));
- end;
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TMPWTool.DoStartProgress;
-
- VAR
- aStr: Str255;
-
- BEGIN
- aStr := fProgName;
- WriteLn(Diagnostic);
- WriteLn(Diagnostic, aStr, ' (Ver ', Version, ') ');
- WriteLn(Diagnostic);
- WriteLn(Diagnostic);
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TMPWTool.ITool;
-
- VAR
- holdIndex: integer;
- prevSig: SignalHandler;
- arg: Str255;
- theDateTime: Longint;
- anAssociation: TAssociation;
-
- BEGIN
- fStartTicks := TickCount;
- GetDateTime(theDateTime);
- fStartDateTime := theDateTime;
-
- fRetCode := RC_Normal;
-
- fInterrupted := false; {becomes True when interrupted}
- fCursorCount := 0; { prepare to spin that cursor}
- SpinCursor(1);
- prevSig := IEsignal(SIGINT, @Intr);
-
- fProgress := false;
- fTime := false;
- fProgName := ArgV^[0]^;
- gProgName := fProgName;
- fRetCode := RC_ParmErrs;
-
- IF fInterrupted THEN
- Stop('');
-
- New(anAssociation);
- FailNil(anAssociation);
- anAssociation.IAssociation;
- fKeyWordList := anAssociation;
-
- InstallKeyWords;
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TRes}
-
- PROCEDURE TMPWTool.Run;
-
- VAR
- fi: FailInfo;
-
- LABEL 1000;
-
- PROCEDURE HdlFailure(error: integer; message: Longint);
-
- VAR
- theErr: OSErr;
- theText: Str255;
-
- BEGIN
- theErr := error;
- IF theErr <> noErr THEN
- BEGIN
- GetSysErrText(theErr, @theText);
- WriteLn(Diagnostic, kErrorMarker, gProgName, ': ', theText);
- fRetCode := RC_Abort;
- END;
- GOTO 1000;
- END;
-
- BEGIN
- CatchFailures(fi, HdlFailure);
- fArgVIndex := 1;
- WHILE fArgVIndex < ArgC DO {ArgC is the number of args plus one}
- BEGIN
- fCursorCount := fCursorCount + 1;
- RotateCursor(fCursorCount);
- ProcessArg(ArgV^[fArgVIndex]^);
- fArgVIndex := fArgVIndex + 1;
- END;
- UnloadSeg(@InitUMPWTool);
- fRetCode := RC_Normal;
-
- IF fProgress THEN
- DoStartProgress;
- DoToolAction;
- IF fTime THEN
- WriteLn(Diagnostic, 'Elapsed time: ', (TickCount - fStartTicks) / 60: 1:
- 2, ' seconds');
- Success(fi);
- 1000:
- IEexit(Ord(fRetCode));
- END;
- {--------------------------------------------------------------------------------------------------}
- {$S TRes}
-
- PROCEDURE TMPWTool.DoToolAction;
- VAR
- aStr: Str255;
-
- BEGIN
- aStr := fProgName;
- WriteLn(Diagnostic, kErrorMarker, aStr,
- ': Forgot to override the default tool action');
- END;
- END.
-